<?php
namespace backend\controllers;

use yii;
use common\helpers\ExcelHelper;
use common\helpers\ArithmeticHelper;
use jianyan\basics\common\models\sys\Provinces;
use jianyan\basics\common\models\sys\Manager;

/**
 * 文件图片上传控制器
 * Class FileController
 * @package backend\modules\sys\controllers
 */
class TestController extends MController
{
    /**
     * 默认禁止使用测试
     * @var bool
     */
    public $visitAuth = true;

    public function init()
    {
        if($this->visitAuth == false)
        {
            die();
        }

        parent::init();
    }

    /**
     * 队列测试
     */
    public function actionQueue()
    {
//        Yii::$app->queue->push(new DownloadJob([
//            'url' => 'http://example.com/image.jpg',
//            'file' => '/tmp/rageframe/image.jpg',
//        ]));

        echo '推送队列成功';

        //将作业推送到队列中延时5分钟运行:
//        Yii::$app->queue->delay(5 * 60)->push(new DownloadJob([
//            'url' => 'http://example.com/image.jpg',
//            'file' => '/tmp/image.jpg',
//        ]));
    }

    /**
     * 上传图片测试
     * @return string
     */
    public function actionUploadImg()
    {
        $model = new Manager();

        return $this->render('upload-img', [
            'model'  => $model,
        ]);
    }

    /**
     * 上传文件测试
     * @return string
     */
    public function actionUploadFile()
    {
        $model = new Manager();

        return $this->render('upload-file', [
            'model'  => $model,
        ]);
    }

    /**
     * 红包生成测试
     */
    public function actionRedPacket()
    {
        //切记如果红包数量太多，不要设置为0.1 会导致最后红包金额不对
        $data = ArithmeticHelper::getRedPackage(100,998,0.01,3);

        $all_money = 0;
        foreach ($data as $datum)
        {
            $all_money += $datum;
        }

        $this->p($all_money);
        $this->p($data);
    }

    /**
     * 测试接口返回时间
     * @return array
     */
    public function actionApiResult()
    {
        $this->setResult();
        $this->_result->code = 200;

        $data = [];
        for ($i = 0; $i < 50; $i++)
        {
            $user   = Manager::find()
                ->with('assignment')
                ->asArray()
                ->all();
        }

        $this->_result->data = $data;

        return $this->getResult();
    }

    /**
     * 写入缓存
     * 经过测试如果设置了缓存为yii系统组件则可以进行写入例下
     *    'cache'      => [
     *        'class' => 'yii\redis\Cache',
     *     ],
     * 一般数据0.0001秒
     * @param $key
     * @param $value
     */
    public function actionSetRedis($key, $value, $time = 60)
    {
        $startTime = microtime(true);
        Yii::$app->redis->set($key, $value);
        //配置过成为yii系统组件可用
        //Yii::$app->cache->set($key, $value, $time);
        $endTime = microtime(true);
        $elapsed = number_format($endTime - $startTime, 4);

        $this->p('写入缓存成功');
        $this->p($elapsed);
    }

    /**
     * 输出redis并获取获得速度
     * @param $key
     */
    public function actionGetRedis($key)
    {
        $startTime = microtime(true);
        $rsp = Yii::$app->redis->get($key);
        $endTime = microtime(true);
        $elapsed = number_format($endTime - $startTime, 4);

        $this->p($rsp);
        $this->p($elapsed);
    }

    /**
     * memcache写入缓存
     * 一般数据0.0005左右
     * @param $key
     * @param $value
     */
    public function actionSetMemCache($key, $value, $time = 60)
    {
        $startTime = microtime(true);
        Yii::$app->memcache->set($key, $value, $time);
        $endTime = microtime(true);
        $elapsed = number_format($endTime - $startTime, 4);

        $this->p('写入缓存成功');
        $this->p($elapsed);
    }

    /**
     * memcache输出测试
     * @param $key
     */
    public function actionGetMemCache($key)
    {
        $startTime = microtime(true);
        $rsp = Yii::$app->memcache->get($key);
        $endTime = microtime(true);
        $elapsed = number_format($endTime - $startTime, 4);

        $this->p($rsp);
        $this->p($elapsed);
    }

    /**
     * 输出phpinfo
     */
    public function actionPhpInfo()
    {
        echo phpinfo();
    }
}